package com.anivia.system.auth.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckOr;
import com.anivia.common.core.constant.PlatformTypeConstant;
import com.anivia.common.core.enums.DesensitizedTypeEnum;
import com.anivia.common.core.model.vo.R;
import com.anivia.common.web.config.desensitization.DataDesensitizationContext;
import com.anivia.common.web.config.tenant.TenantContext;
import com.anivia.system.auth.model.dto.UserInfoLoginDTO;
import com.anivia.system.auth.model.vo.UserLoginVO;
import com.anivia.system.auth.service.IUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * <p>
 * 用户基础信息 前端控制器
 * </p>
 *
 * @author wangj
 * @since 2024-04-02
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("common/user")
public class UserInfoCommonController {
    @Resource
    private IUserInfoService userInfoService;

    @Operation(summary = "登录")
    @PostMapping("login")
    private R<UserLoginVO> login(@RequestBody @Valid UserInfoLoginDTO dto) {
        TenantContext.ignore();
        return R.ok(userInfoService.login(dto));
    }

    @Operation(summary = "登出")
    @PostMapping("logout")
    @SaCheckOr(
            login = {@SaCheckLogin(type = PlatformTypeConstant.USER)
                    , @SaCheckLogin(type = PlatformTypeConstant.TENANT)
                    , @SaCheckLogin(type = PlatformTypeConstant.ADMIN)}
    )
    private R logout() {
        userInfoService.logout();
        return R.ok();
    }

    @Operation(summary = "查询登录信息")
    @PostMapping("userInfo")
    @SaCheckOr(
            login = {@SaCheckLogin(type = PlatformTypeConstant.USER)
                    , @SaCheckLogin(type = PlatformTypeConstant.TENANT)
                    , @SaCheckLogin(type = PlatformTypeConstant.ADMIN)}
    )
    private R<UserLoginVO> userInfo() {
        DataDesensitizationContext.startCustom(Set.of(DesensitizedTypeEnum.PHONE, DesensitizedTypeEnum.CHINESE_NAME));
        return R.ok(userInfoService.userInfo());
    }
}
